package com.shop.model.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.model.entity.Classifys;
import com.shop.model.entity.GoodsTb;
import com.shop.model.entity.Species;
import com.shop.model.mapper.GoodsTbMapper;
import com.shop.model.service.IClassifysService;
import com.shop.model.service.IGoodsTbService;
import com.shop.model.service.ISpeciesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shop/叶虽
 * @since 2018-12-12
 */
@Controller
public class GoodsTbController {
    @Autowired
    private IGoodsTbService iGoodsTbService;
    @Autowired
    private GoodsTbMapper goodsTbMapper;
    @Autowired
    private IClassifysService iClassifysService;
    @Autowired
    private ISpeciesService iSpeciesService;

    /**
     * @author 叶虽/根据分类id查询商品
     * @param page
     * @param speciesId
     * @return
     */
    @ResponseBody()
    @RequestMapping(value = "/goodsController/goodslist")
    public Object goodslist(String page,String speciesId){
        Integer integer = Integer.valueOf(page);
        IPage iPage = goodsTbMapper.selectPage(new Page(integer, 6)
                , new QueryWrapper<GoodsTb>()
                .eq("specie_id",speciesId));

        List<GoodsTb> goodsTbList = iPage.getRecords();
        return goodsTbList;
    }

    /**
     * @author 叶虽/显示价格升序商品
     * @param page
     * @return
     */
    @ResponseBody()
    @RequestMapping(value = "/goodsController/priceAscendingOrder")
    public Object priceAscendingOrder(String page){
        Integer integer = Integer.valueOf(page);
        IPage iPage = goodsTbMapper.selectPage(new Page(integer, 6)
                , new QueryWrapper<GoodsTb>()
                .orderByAsc("price"));

        List<GoodsTb> priceAsc = iPage.getRecords();
        return priceAsc;
    }
    /**
     * @author 叶虽/显示价格降序商品
     * @param page
     * @return
     */
    @ResponseBody()
    @RequestMapping(value = "/goodsController/priceDescendingOrder")
    public Object priceDescendingOrder(String page){
        Integer integer = Integer.valueOf(page);
        IPage iPage = goodsTbMapper.selectPage(new Page(integer, 6)
                , new QueryWrapper<GoodsTb>()
                .orderByDesc("price"));

        List<GoodsTb> priceDesc = iPage.getRecords();
        return priceDesc;
    }
    /**
     * @author 叶虽/显示最新商品
     * @param page
     * @return
     */
    @ResponseBody()
    @RequestMapping(value = "/goodsController/newProducts")
    public Object newProducts(String page){
        Integer integer = Integer.valueOf(page);
        IPage iPage = goodsTbMapper.selectPage(new Page(integer, 6)
                , new QueryWrapper<GoodsTb>()
                .orderByDesc("shelves_date"));

        List<GoodsTb> goodsTbList = iPage.getRecords();
        return goodsTbList;
    }
    /**
     * @author 叶虽/显示搜索商品
     * @param page
     * @return
     */
    @ResponseBody()
    @RequestMapping(value = "/goodsController/searchShopping")
    public Object searchShopping(String page ,String content){
        Integer integer = Integer.valueOf(page);
        IPage iPage = goodsTbMapper.selectPage(new Page(integer, 6)
                , new QueryWrapper<GoodsTb>()
               .like("goods_name",content));
        List<GoodsTb> contentList = iPage.getRecords();
        return contentList;
    }
    /**
     * @author 叶虽/显示搜索商品
     * @param
     * @return
     */

    @RequestMapping(value = "/goodsController/indexSearch")
    public String indexSearch(Model model, String content, HttpServletRequest request){
        //显示总分类名
        List<Classifys> classifysList = iClassifysService.list();
        //显示分类名
        List<Species> speciesList = iSpeciesService.list();


        List<GoodsTb> goodsTbList = iGoodsTbService.list(new QueryWrapper<GoodsTb>().like("goods_name", content));
        for (GoodsTb goodsTb : goodsTbList) {
            String goodsName = goodsTb.getGoodsName();
            System.out.println("查询到东西：" + goodsName);
        }
        model.addAttribute("classify",classifysList);
        model.addAttribute("speciesList",speciesList);
        model.addAttribute("content",goodsTbList);
        return "commodity";
    }
    @RequestMapping(value = "/specieIddSearch")
    public String idSearch(Model model, String content, HttpServletRequest request){
        //显示总分类名
        List<Classifys> classifysList = iClassifysService.list();
        //显示分类名
        List<Species> speciesList = iSpeciesService.list();
        List<GoodsTb> goodsTbList = iGoodsTbService.list(new QueryWrapper<GoodsTb>().eq("specie_id",content));
        model.addAttribute("classify",classifysList);
        model.addAttribute("speciesList",speciesList);
        model.addAttribute("content",goodsTbList);
        return "commodity";
    }
}

